package com.xiaoxin.dubbo.api;

import com.mongodb.client.result.DeleteResult;
import com.xiaoxin.model.enums.CommentType;
import com.xiaoxin.model.mongo.Comment;
import com.xiaoxin.model.mongo.Movement;
import com.xiaoxin.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author xiaoxiaode
 * @date 2021-09-05-22:46
 **/
@DubboService
@SuppressWarnings("all")
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment) {
        // 查询动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        // 向comment对象设置被评论人属性
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }
        // 保存到数据库
        mongoTemplate.save(comment);
        // 更新动态表中的对应数据
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        // 设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement movement1 = mongoTemplate.findAndModify(query, update, options, Movement.class);
        // 获取最新的评论数量并返回

        return movement1.statisCount(comment.getCommentType());
    }

    @Override
    public List<Comment> findComments(Integer page, Integer pagesize, CommentType commentType, String movementId) {
        Query query = Query.query(Criteria.where("publishId")
                .is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType()))
                .skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Comment.class);
    }

    @Override
    public boolean hasComment(String movementId, Long userId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        // 判断数据是否存在
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer remove(Comment comment) {
        // 删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);
        // 修改动态表中的总数量
        Query momentQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        // 设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(momentQuery, update, options, Movement.class);
        // 获取最新的评论数量并返回
        return modify.statisCount(comment.getCommentType());
    }

    @Override
    public List<Comment> findById(Long userId, CommentType commentType) {
        Query query = Query.query(Criteria
                .where("publishUserId").is(userId).and("commentType").is(commentType.getType()))
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public boolean isLike(Long userId, ObjectId videoId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(videoId)
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public void saveVideoLike(Comment comment) {
        mongoTemplate.save(comment);
        Query videoQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
            // 设置更新参数
            FindAndModifyOptions options = new FindAndModifyOptions();
            options.returnNew(true);
            mongoTemplate.findAndModify(videoQuery, update, options, Video.class);
        }
    }

    @Override
    public void deleteVideoLike(Comment comment,Long userId) {
        Query query=Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(userId));
        DeleteResult result = mongoTemplate.remove(query,Comment.class);
        System.out.println(result);
        Query videoQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
            // 设置更新参数
            FindAndModifyOptions options = new FindAndModifyOptions();
            options.returnNew(true);
            mongoTemplate.findAndModify(videoQuery, update, options, Video.class);
        }
    }


    @Override
    public Integer delete(Comment comment) {
        // 删除Comment表数据
        Criteria criteria = Criteria.where("userId")
                .is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        // 修改评论表中数量
        Query momentQuery = Query.query(Criteria.where("id").is(comment.getPublishId())
                .and("commentType").is(CommentType.COMMENT.getType()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        }
        // 设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment1 = mongoTemplate.findAndModify(momentQuery, update, options, Comment.class);
        mongoTemplate.remove(query, Comment.class);
        // 获取最新的点赞数量并返回
        return comment1.getLikeCount();
    }

    @Override
    public Integer saveComment(Comment comment, ObjectId publishId) {
        Query query1 = Query.query(Criteria.where("id").is(publishId).and("commentType").is(CommentType.COMMENT.getType()));
        Comment commented = mongoTemplate.findOne(query1, Comment.class);
        comment.setPublishUserId(commented.getUserId());
        mongoTemplate.save(comment);
        // 更新表中的对应数据
        // Query query2 = Query.query(Criteria.where("id").is(comment.getId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        }
        // 设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment1 = mongoTemplate.findAndModify(query1, update, options, Comment.class);
        // 获取最新的评论数量并返回
        return comment1.getLikeCount();
    }
}
